//
//  drawerViewController.m
//  ziLu_ios
//
//  Created by mac on 16/2/22.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "drawerViewController.h"
#import "MY_TableViewCell.h"
#import "gongNengTableViewCell.h"
#import "FriendGroup.h"
#import "Friend.h"
#import "HeadView.h"


@interface drawerViewController ()<UITableViewDataSource,UITableViewDelegate,HeadViewDelegate>
{
    NSArray *_friendsData;
    UIImageView *Dimage;
    UILabel *Dlabel;
}
@end

@implementation drawerViewController 

- (void)viewDidLoad {
    [super viewDidLoad];
    // 接收push设置页面的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cell:) name:@"image" object:nil];
    _table = [[UITableView alloc]initWithFrame:CGRectMake(0,90, MAIN_WIDTH, self.view.bounds.size.height-95)];
    _table.backgroundColor = [UIColor colorWithRed:249/255.0 green:249/255.0 blue:249/255.0 alpha:1];
    self.edgesForExtendedLayout=NO;
    _table.dataSource =self;
    _table.delegate =self;
    _table.sectionHeaderHeight = 50;//g高度

    self.table.separatorStyle = UITableViewStylePlain;
    [self.view addSubview:_table];
    [self loadData];
    
    UINib *nib3= [UINib nibWithNibName:@"gongNengTableViewCell" bundle:nil];
    [self.table registerNib:nib3 forCellReuseIdentifier:@"gongNengTableViewCell"];//注册
    
    
    //头像页
    UIView *Drawerview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MAIN_WIDTH, 90)];
    Drawerview.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:Drawerview];
    
    Dimage = [[UIImageView alloc]initWithFrame:CGRectMake(15, 19, 60, 60)];
    Dimage.layer.cornerRadius = 30;
    [Dimage.layer setMasksToBounds:YES];
    Dimage.layer.borderColor = [UIColor grayColor].CGColor;
    Dimage.layer.borderWidth = 0.5f;
    Dimage.image=[UIImage imageNamed:@"克林埃尔.png"];
    [Drawerview addSubview:Dimage];
    
    Dlabel=[[UILabel alloc]initWithFrame:CGRectMake(95, 47, 90, 20)];
    Dlabel.backgroundColor=[UIColor whiteColor];
    Dlabel.text=@"克林埃尔";
    Dlabel.textColor = [UIColor grayColor];
    Dlabel.font = [UIFont fontWithName:@"Courier-Bold" size:15];
    [Drawerview addSubview:Dlabel];
    
    
    [Drawerview addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myInfo)]];
    
    
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if (![[LoginSingleton sharedLoginSingleton].token isEqualToString:@""] && [LoginSingleton sharedLoginSingleton].token != nil) {
        
        [Dimage sd_setImageWithURL:[NSURL URLWithString:Get_HeadImage_UrlString([[LoginSingleton sharedLoginSingleton].loginModel avatar])] placeholderImage:[UIImage imageNamed:@"个人注册.png"]];
        
        Dlabel.text = [[LoginSingleton sharedLoginSingleton].loginModel nickname];
    }else{
        Dimage.image=[UIImage imageNamed:@"克林埃尔.png"];
        Dlabel.text=@"克林埃尔";
    }
}


#pragma mark -- 点击头像  弹出个人信息
- (void)myInfo{

    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushMyInfo" object:nil];
}


//组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    

   return _friendsData.count;
}
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    

    FriendGroup *friendGroup = _friendsData[section];
    NSInteger count = friendGroup.isOpened ? friendGroup.child.count : 0;
    return count;
    
}
//自定义cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

        gongNengTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"gongNengTableViewCell" forIndexPath:indexPath];
    
    FriendGroup *friendGroup = _friendsData[indexPath.section];
    Friend *friend = friendGroup.child[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.namelabel.text = friend.name;
    NSString*str=[NSString stringWithFormat:@"(%@)",friend.count];
    cell.numberlabel.text = str;

        return cell;
    
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    HeadView *headView = [HeadView headViewWithTableView:tableView];
    
    
    headView.delegate = self;
    headView.friendGroup = _friendsData[section];
    
    return headView;
}




//点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;{
    FriendGroup *friendGroup = _friendsData[indexPath.section];
     Friend *friend = friendGroup.child[indexPath.row];
//    NSLog(@"传值ID%@",friend.id);
    
}


- (void)clickHeadView
{
    [self.table reloadData];
}




-(void)cell:(NSNotification*)cell{
    
    
    if (![[LoginSingleton sharedLoginSingleton].token isEqualToString:@""] && [LoginSingleton sharedLoginSingleton].token != nil) {
        
        [Dimage sd_setImageWithURL:[NSURL URLWithString:Get_HeadImage_UrlString([[LoginSingleton sharedLoginSingleton].loginModel avatar])] placeholderImage:[UIImage imageNamed:@"个人注册.png"]];
        
        Dlabel.text = [[LoginSingleton sharedLoginSingleton].loginModel nickname];
    }

}

// 移除通知
- (void)removeObserver:(id)observer{

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}



#pragma mark 加载数据
- (void)loadData
{

    __weak __typeof(self) weakSelf = self;
 [HTTPManager get:@"http://cleanerair.com.cn/Api/Menu/list" success:^(AFHTTPRequestOperation *operation, id responseObject) {
     
     NSArray*dataArr=[responseObject objectForKey:@"date"];
//     NSLog(@"%@",dataArr);
     NSMutableArray *fgArray = [NSMutableArray array];
     for (NSDictionary*dict in dataArr) {
         FriendGroup *friendGroup = [FriendGroup friendGroupWithDict:dict];
         [fgArray addObject:friendGroup];
     }
     _friendsData = fgArray;
    [self.table reloadData];
 } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     
     [weakSelf hideIndicator];
    
 }];
    
}




@end
